// miniprogram/pages/searchFile/searchFile.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    input:0,//鼠标聚焦
    keyword:"",//搜索内容

    fileCollection: [],
    filesDisplay:[],//显示的文件
    start:0,//渲染开始位置
    skipNum:10,//追加数据数量
    count: 0, //文件总数

    //筛选
    all: 1,
    mySchool: 0,
    myFaculty: 0,
    gold: 0,
    down: 0, //1上2下

    //用户信息
    user_school: "",
    user_faculty: "",
  },
 //搜索
 input:function(e){
  var that = this
  that.setData({
    keyword:e.detail.value
  })
},
search:function(){
   var that = this
   wx.showLoading({
    title: '加载中...',
  })

  wx.request({
    url:  'https://www.cerywxr.com/treehole/index.php/Home/ques/search_all_file',
    data: { 
      keyword: that.data.keyword
    },
    method: 'POST',
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    success(res) {
      if (res.data.error_code == 0) {
        
        if (res.data.count > 100) {
          that.setData({
            fileCollection: res.data.data,
            count: "99+"
          })
        } else {
          that.setData({
            fileCollection: res.data.data,
            count: res.data.count,
            gold: 0,
            down: 0, //1上2下
          })
        }
        that.setData({
          //筛选
          all: 1,
          mySchool: 0,
          myFaculty: 0,
          filesDisplay:that.data.fileCollection.slice(0,that.data.skipNum),
          start:that.data.skipNum
        })
      } else if(res.data.error_code == 1){
        wx.showModal({
          title: '提示!',
          content: '搜索内容不得为空',
          showCancel: false,
          success(res) { }
        })
      }
    },
    fail: function (res) {
      wx.showModal({
        title: '哎呀~!',
        content: '网络状态不佳呢！',
        showCancel: false,
        success(res) { }
      })
    },
    complete: function (res) {   //注意！
      wx.hideLoading()
    }
  })
},
  //鼠标聚焦
  focus:function(){
    this.setData({
      input:1
    })
  },

  //鼠标移开
  blur:function(){
    this.setData({
      input:0
    })
  },

  //页面初始化
  onShow: function (options) {
    var that = this
    wx.request({
      url: 'https://www.cerywxr.com/treehole/index.php/Home/sharing/get_all_files',
      method: "POST", //请求方式,
      success(res) {
        console.log(res.data)
        if (res.data.count > 100) {
          that.setData({
            fileCollection: res.data.data,
            count: "99+"
          })
        } else {
          that.setData({
            fileCollection: res.data.data,
            count: res.data.count
          })
        }
        that.setData({
          filesDisplay:that.data.fileCollection.slice(0,that.data.skipNum),
          start:that.data.skipNum,
          user_school: getApp().globalData.userInfo.user_school,
          user_faculty: getApp().globalData.userInfo.user_faculty,
        })
      }
    })
  },


  //触底刷新，获取更多
  onReachBottom:function(){
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    var end=that.data.start+that.data.skipNum
    var newData=that.data.fileCollection.slice(that.data.start,end)
    that.setData({
      filesDisplay:that.data.filesDisplay.concat(newData),//拼接数组
      start:end
    })
    setTimeout(() => {
      wx.hideLoading()
    }, 500);//计时器  
  },

  //筛选条件
  //全部
  All: function () {
    var that = this
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: 'https://www.cerywxr.com/treehole/index.php/Home/sharing/get_all_files',
      method: "POST", //请求方式,
      header: {
        'content-type': 'application/x-www-form-urlencoded' //POST提交数据方式
      },
      success(res) {
        console.log(res.data)
        if (res.data.count > 100) {
          that.setData({
            fileCollection: res.data.data,
            count: "99+"
          })
        } else {
          that.setData({
            fileCollection: res.data.data,
            count: res.data.count,
            gold: 0,
            down: 0, //1上2下
          })
        }
        that.setData({
          //筛选
          all: 1,
          mySchool: 0,
          myFaculty: 0,
          filesDisplay:that.data.fileCollection.slice(0,that.data.skipNum),
          start:that.data.skipNum
        })
        wx.hideLoading()
      }
    })
  },

  //本学院
  Faculty: function () {
    var that = this
    console.log(that.data.user_school)
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: 'https://www.cerywxr.com/treehole/index.php/Home/sharing/get_myFaculty_files',
      method: "POST", //请求方式,
      header: {
        'content-type': 'application/x-www-form-urlencoded' //POST提交数据方式
      },
      data: {
        user_school: that.data.user_school,
        user_faculty: that.data.user_faculty
      },
      success(res) {
        console.log(res.data)
        if (res.data.count > 100) {
          that.setData({
            fileCollection: res.data.data,
            count: "99+"
          })
        } else {
          that.setData({
            fileCollection: res.data.data,
            count: res.data.count,
          })
        }
        that.setData({
          //筛选
          all: 0,
          mySchool: 0,
          myFaculty: 1,
          gold: 0,
          down: 0, //1上2下
          filesDisplay:that.data.fileCollection.slice(0,that.data.skipNum),
          start:that.data.skipNum
        })
        wx.hideLoading()
      }
    })
  },

  //本学校
  School: function () {
    var that = this
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: 'https://www.cerywxr.com/treehole/index.php/Home/sharing/get_mySchool_files',
      method: "POST", //请求方式,
      header: {
        'content-type': 'application/x-www-form-urlencoded' //POST提交数据方式
      },
      data: {
        user_school: that.data.user_school
      },
      success(res) {
        console.log(res.data)
        if (res.data.count > 100) {
          that.setData({
            fileCollection: res.data.data,
            count: "99+"
          })
        } else {
          that.setData({
            fileCollection: res.data.data,
            count: res.data.count,
          })
        }
        that.setData({
          //筛选
          all: 0,
          mySchool: 1,
          myFaculty: 0,
          gold: 0,
          down: 0, //1上2下
          filesDisplay:that.data.fileCollection.slice(0,that.data.skipNum),
          start:that.data.skipNum
        })
        wx.hideLoading()
      }
    })
  },

  //金币
  Gold: function () {
    var that = this
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: 'https://www.cerywxr.com/treehole/index.php/Home/sharing/get_gold_files',
      method: "POST", //请求方式,
      header: {
        'content-type': 'application/x-www-form-urlencoded' //POST提交数据方式
      },
      data: {
        user_school: that.data.user_school,
        user_faculty: that.data.user_faculty,
        //筛选
        mySchool: that.data.mySchool,
        myFaculty: that.data.myFaculty,
        gold: (that.data.gold + 1) % 3,
      },
      success(res) {
        console.log(res.data)
        if (res.data.count > 100) {
          that.setData({
            fileCollection: res.data.data,
            count: "99+"
          })
        } else {
          that.setData({
            fileCollection: res.data.data,
            count: res.data.count,
          })
        }
        that.setData({
          //筛选
          down:0,
          gold: (that.data.gold + 1) % 3,
          filesDisplay:that.data.fileCollection.slice(0,that.data.skipNum),
          start:that.data.skipNum
        })
        wx.hideLoading()
      }
    })
  },

  //下载量
  Down:function(){
    var that = this
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: 'https://www.cerywxr.com/treehole/index.php/Home/sharing/get_down_files',
      method: "POST", //请求方式,
      header: {
        'content-type': 'application/x-www-form-urlencoded' //POST提交数据方式
      },
      data: {
        user_school: that.data.user_school,
        user_faculty: that.data.user_faculty,
        //筛选
        mySchool: that.data.mySchool,
        myFaculty: that.data.myFaculty,
        down: (that.data.down + 1) % 3,
      },
      success(res) {
        console.log(res.data)
        if (res.data.count > 100) {
          that.setData({
            fileCollection: res.data.data,
            count: "99+"
          })
        } else {
          that.setData({
            fileCollection: res.data.data,
            count: res.data.count,
          })
        }
        that.setData({
          //筛选
          gold:0,
          down: (that.data.down + 1) % 3,
          filesDisplay:that.data.fileCollection.slice(0,that.data.skipNum),
          start:that.data.skipNum
        })
        wx.hideLoading()
      }
    })
  },


})